package com.example.homework8.fragment;

import android.app.Activity;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.homework8.R;
import com.example.homework8.utils.SharedUtil;

import static android.content.ContentValues.TAG;


public class MeFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private Activity activity;
    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public MeFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MeFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MeFragment newInstance(String param1, String param2) {
        MeFragment fragment = new MeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        this.activity = getActivity();
    }
    TextView nametv,phone,classname;
    ImageView imageView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view =  inflater.inflate(R.layout.fragment_me, container, false);
        nametv= (TextView) view.findViewById(R.id.name);
        imageView = view.findViewById(R.id.imgview);
        Log.d(TAG, "onCreateView: img filepath" + SharedUtil.getIntance(getActivity()).readShared("imgurl",""));
        imageView.setImageBitmap(BitmapFactory.decodeFile(SharedUtil.getIntance(getActivity()).readShared("imgurl","")));
        nametv.setText(SharedUtil.getIntance(activity).readShared("loginuser","未登录"));

        phone= (TextView) view.findViewById(R.id.birth);
        phone.setText(SharedUtil.getIntance(activity).readShared("phone","未登录"));

        classname= (TextView) view.findViewById(R.id.classname);
        classname.setText(SharedUtil.getIntance(activity).readShared("classname","未登录"));
        Button edit1 = view.findViewById(R.id.editinfo);
        edit1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goedit();
            }
        });

        return  view;
    }
    public void goedit (){
        getActivity().getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.id_content, new UserInfoFragment(), null)
                .addToBackStack(null)
                .commit();
    }
}